import {Component, OnInit} from '@angular/core';
import {UserServiceService} from '../user-service.service';

@Component({
  selector: 'app-user-table-copy',

  templateUrl: './user-table-copy.component.html',
  styleUrls: ['./user-table-copy.component.less']
})
export class UserTableCopyComponent implements OnInit {

  constructor(private userService: UserServiceService) {
  }

  /***************表名集合****************** */
  select = []                   // 表名集合
  selectMaster: any = []               // 选择的主表表名
  selectNew: any = []                  // 选择的新表表名
  /***************主表****************** */
  master = []                 //【主表】
  columns: any = []              //【主表】列名
  selectedMaster = ''           // 切换的【主表】
  currentpage: number = 1        //【主表】当前页
  thispage: number = 8           //【主表】当前页记录数
  totalrow: number = 0           //【新表】总记录数
  /***************新表****************** */
  new = []                    //【新表】
  columnsNew: any = []           //【主表】列名
  selectedNew = ''              // 切换的【新表】
  newcurrentpage: number = 0    //【新表】当前页
  newthispage: number = 0        //【新表】当前页记录数
  newtotalrow: number = 0        //【新表】总记录数


  /**
   * 加载数据
   */
  ngOnInit() {
    this.getTableNames()
    setTimeout(() => { //堵塞flag
      this.selectedMaster = this.select[0]
      this.getAllDate(this.selectedMaster, 1, 8)
    }, 500);

  }

  /**
   * 获取数据库下所有表名
   */
  getTableNames() {
    this.userService.getTableNames().subscribe((res) => {
      this.select = res.data.tableName
    })
  }

  /**
   * 加载出默认的主表
   */
  getAllDate(tableName: any, current: number, limit: number) {
    this.userService.getTable(tableName, current, limit).subscribe((res) => {
      this.master = res.data.rows;
      this.currentpage = current //当前页
      this.thispage = limit
      this.totalrow = res.data.total//一共多少条
      var a = []
      for (let i = 0; i < res.data.field.data.tableName.length; i++) {
        a.push({
          name: res.data.field.data.tableName[i],
          prop: res.data.field.data.tableName[i],
        })
      }
      this.columns = a
    })
  }

  /**
   * 切换主表
   */
  changeTableMaster(event: any) {
    this.currentpage = 1;
    this.thispage = 8;
    this.userService.getTable(event, this.currentpage, this.thispage).subscribe((res) => {
      this.master = res.data.rows;
      this.currentpage = this.currentpage //当前页
      this.thispage = this.thispage
      this.totalrow = res.data.total//一共多少条
      var a = []
      for (let i = 0; i < res.data.field.data.tableName.length; i++) {
        a.push({
          name: res.data.field.data.tableName[i],
          prop: res.data.field.data.tableName[i],
        })

      }
      this.columns = a
    })
  }

  /**
   *
   * @param event 切换新表页码数
   */
  changePageMaster(event: any) {
    console.log(this.selectedMaster + event.page + this.thispage)
    this.getAllDate(this.selectedMaster, event.page, this.thispage)
  }

  /**
   * 加载出默认的主表
   */
  getAllDateNew(tableName: any, current: number, limit: number) {
    this.userService.getTable(tableName, current, limit).subscribe((res) => {
      this.new = res.data.rows;
      this.newcurrentpage = current //当前页
      this.newthispage = limit
      this.newtotalrow = res.data.total//一共多少条
      var a = []
      for (let i = 0; i < res.data.field.data.tableName.length; i++) {
        a.push({
          name: res.data.field.data.tableName[i],
          prop: res.data.field.data.tableName[i],
        })
      }
      this.columnsNew = a
    })
  }

  /**
   * 切换副表
   * @param event
   */
  changeTableNew(event: any) {
    this.newcurrentpage = 1;
    this.newthispage = 8;
    this.userService.getTable(event, this.newcurrentpage, this.newthispage).subscribe((res) => {
      this.new = res.data.rows;
      this.newcurrentpage = this.newcurrentpage //当前页
      this.newthispage = this.thispage
      this.newtotalrow = res.data.total//一共多少条
      var a = []
      for (let i = 0; i < res.data.field.data.tableName.length; i++) {
        a.push({
          name: res.data.field.data.tableName[i],
          prop: res.data.field.data.tableName[i],
        })
      }
      this.columnsNew = a
    })
  }

  /**
   *
   * @param event 切换新表页码数
   */
  changePageNew(event: any) {
    console.log(this.selectedNew + event.page + this.newthispage)
    this.getAllDateNew(this.selectedNew, event.page, this.newthispage)
  }

  /**
   *
   * @param event 勾选列名
   */
  changeMaster(event: any) {
    if (event.srcElement.checked) {
      this.selectMaster.push(event.srcElement.value)
    }

    if (event.srcElement.checked == false) {
      this.selectMaster = this.selectMaster.filter((item: any) => item != event.srcElement.value)
    }
    console.log("this.selectMaster"+this.selectMaster)
  }

  /**
   *
   * @param event 勾选列名
   */
  changeNew(event: any) {
    if (event.srcElement.checked == true) {
      this.selectNew.push(event.srcElement.value)
    }
    if (event.srcElement.checked == false) {
      this.selectNew = this.selectNew.filter((item: any) => item != event.srcElement.value)
    }
    console.log(" this.selectNew"+ this.selectNew)
  }

  /**
   * 同步
   */
  translation() {
    let filedArrays = {
      queryTableName: this.selectedMaster,
      newTableName: this.selectedNew,
      tableMaster: this.selectMaster,
      tableNew: this.selectNew
    }

    this.userService.copy(filedArrays).subscribe((res)=>{
      console.log(res)
    })
    location.reload()
  }



}
